package utils;

import java.text.Normalizer;
import java.text.Normalizer.Form;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JOptionPane;

public class Debug {
	
	public static boolean DEBUG;
	public static String TAG;
	public static int LOG_INFO=0;
	public static int LOG_WARNING=1;
	public static int LOG_ERREUR=2;
	
	public static void start(String[] args) {
		DEBUG=false;
		TAG="simple";
		if (args.length>0) {
			for (int i=0;i<args.length;i++) {
				if (args[i].substring(0,7).equals("-debug=")) {
					String value=args[i].substring(7);
					DEBUG=(value.equals("true"))?true:false;
				} else if (args[i].substring(0,5).equals("-tag=")) {
					TAG=removeAccents(args[i].substring(5).toLowerCase());
				}
			}
		}
	}
	public static void addLog(String text,int MessageType,String tag) {
		if (DEBUG) {
			tag=removeAccents(tag);
			
			if (tag.equals("all") || TAG.contains(tag.toLowerCase()) || MessageType==LOG_ERREUR || TAG.equals("all")) {
				Date d = new Date();
				String l;
				switch (MessageType) {
				case 0  : l=" info  ";break;
				case 1  : l="Warning";break;
				case 2  : l="ERREUR ";break;
				default : l="  log  ";
				}
				if (MessageType==LOG_ERREUR)
					System.err.println("["+l+"] "+dateLog(d)+" : "+text);
				else
					System.out.println("["+l+"] "+dateLog(d)+" : "+text);
			}
		}
	}
	public static boolean verrou() {
		//if (!Debug.verrou()) {}
		int year = 2010;
		int month = 11;
		int day = 26;
		
		Calendar today = Calendar.getInstance();
		today.setTime(new Date());
		Calendar fin = Calendar.getInstance();
		fin.set(year,month-1,day);
		
		if (today.before(fin)) {
			JOptionPane.showMessageDialog(null, "Ceci est une version Bêta à durée limité.");
			return false;
		} else {
			JOptionPane.showMessageDialog(null, "Cette version Bêta à expirée.");
			return true;
		}
	}
	
	static String removeAccents(String text) {
		return  Normalizer.normalize(text,Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
	}
	static String dateLog(Date d) {
		String res="";
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		res+=(cal.get(Calendar.HOUR_OF_DAY)<10)?"0"+cal.get(Calendar.HOUR_OF_DAY):cal.get(Calendar.HOUR_OF_DAY);
		res+="h";
		res+=(cal.get(Calendar.MINUTE)<10)?"0"+cal.get(Calendar.MINUTE):cal.get(Calendar.MINUTE);
		res+="m";
		res+=(cal.get(Calendar.SECOND)<10)?"0"+cal.get(Calendar.SECOND):cal.get(Calendar.SECOND);
		res+="s";
		int m=cal.get(Calendar.MILLISECOND);String r="";if (m<10) r+="0";if (m<100) r+="0";r+=m;
		res+=r;
		return res;
	}
}